using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Xna.Framework;
using BoOnce.Components;
using Microsoft.Xna.Framework.Graphics;
using BoOnce.Core.Components;

namespace BoOnce.Menu
{
    class Menu
    {

        private Dictionary<String,MenuScreen> _Screens;
        private MenuScreen _CurrentScreen;
        private MenuScreen _NextScreen;
        private SpriteBatch _SpriteBatch;
        private InputComponent _InputComponent;
        private Boolean _TransitionOutRequested = false;
        private Boolean _TransitionGoNextRequested = false;

        public Menu(Game game)
        {
            _SpriteBatch = new SpriteBatch(game.GraphicsDevice);
            _Screens = new Dictionary<string, MenuScreen>();
            _CurrentScreen = null;
            _InputComponent = (InputComponent)BaseGame.GetInstance().Services.GetService(typeof(InputComponent));
        }

        
        public void Add(String name,MenuScreen screen)
        {
            _Screens.Add(name, screen);
            screen.LoadContent();
            if (_CurrentScreen == null) _CurrentScreen = screen;
        }

        public void TransitionTo(String menuTo)
        {
            if (_Screens.TryGetValue(menuTo, out _NextScreen))
            {
                _TransitionOutRequested = true;
                _TransitionGoNextRequested = false;
            }
        }

        public void Update(GameTime gameTime)
        {
            if (_CurrentScreen == null) return;

            _CurrentScreen.Update(gameTime);

            // on ne peut pas changer de choix pendant une transition
            if (_CurrentScreen.State == MenuScreen.ScreenStates.TransitionFinished)
            {
                if (_InputComponent.IsNewPress(PlayerIndex.One, InputComponent.Buttons.Down))
                    _CurrentScreen.HighlightNextEntry();

                if (_InputComponent.IsNewPress(PlayerIndex.One, InputComponent.Buttons.Up))
                    _CurrentScreen.HighlightPreviousEntry();

                if (_InputComponent.IsNewPress(PlayerIndex.One, InputComponent.Buttons.Action1))
                    _CurrentScreen.SelectHighlightedEntry();
                
            }

            // Manage transition between 2 screens
            if (_TransitionOutRequested && _CurrentScreen.State != MenuScreen.ScreenStates.TransitionOut )
            {
                _CurrentScreen.ChangeState(MenuScreen.ScreenStates.TransitionOut, gameTime);
                _TransitionOutRequested = false;
                _TransitionGoNextRequested = true;
            }

            if (_TransitionGoNextRequested && _CurrentScreen.State == MenuScreen.ScreenStates.TransitionFinished)
            {
                _CurrentScreen = _NextScreen;
                _TransitionGoNextRequested = false;
                _CurrentScreen.ChangeState(MenuScreen.ScreenStates.TransitionIn, gameTime);
            }
        }

        public void Draw(GameTime gameTime)
        {
            if (_CurrentScreen == null) return;
            _CurrentScreen.Draw(gameTime);
        }

    }
}
